/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.figures;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.gefcommon.common.resourceManager.ColorManager;
import edu.pku.sei.gefcommon.common.resourceManager.ResourceGroupManager;
/**
 * @author jesse
 *
 * 2006-3-20
 */
public class MetaCommentFigure extends CommonFigure {
//	public static final Color COMMENT_COLOR = new Color(null,220,220,255);
	public static final Color COMMENT_COLOR = new Color(null,255,255,255);
	TextFlow textFlow;
	public int width;
	private int cornerSize = 10;
	
	public MetaCommentFigure(IFigure header){
/*		
		this.header = header;
		ToolbarLayout layout = new ToolbarLayout();
		setLayoutManager(layout);	
		setBorder(new CompoundBorder(new LineBorder(ColorConstants.buttonDarker,1), new MarginBorder(1)));
//		setBackgroundColor(new Color(null,255,255,206));
		setBackgroundColor(new Color(null,100,100,100));
		setOpaque(true);
		body = new Figure();
		body.setLayoutManager(new ToolbarLayout());
		contentCompart = new CompartmentFigure();
		body.add(contentCompart);
		rectCompart = new CompartmentFigure();
		body.add(rectCompart);
		add(body);
*/
		int borderSize = 10;
		setBorder(new MarginBorder(borderSize));
		FlowPage flowPage = new FlowPage();

		this.textFlow = new TextFlow();

		textFlow.setLayoutManager(new ParagraphTextLayout(textFlow,
						ParagraphTextLayout.WORD_WRAP_TRUNCATE));

		flowPage.add(textFlow);

		setLayoutManager(new StackLayout());
		setBackgroundColor(COMMENT_COLOR);
		add(flowPage);

	}
	@Override
	protected void paintShadow(Graphics g) {
		g.pushState();
		Rectangle rect = getBounds().getCopy().translate(2, cornerSize+2);
		rect.height -= cornerSize;
		g.setClip(rect);
		g.setBackgroundColor(ColorConstants.buttonDarker);
		g.fillRectangle(rect);
		g.popState();
	}
	protected void paintFigure(Graphics graphics) {
		paintShadow(graphics);
		Rectangle rect = getBounds().getCopy();

		this.width = rect.width;
		// fill the note
		PointList outline = new PointList();
		
		outline.addPoint(0, 0);
		outline.addPoint(rect.width - cornerSize, 0);
		outline.addPoint(rect.width - 1, cornerSize);
		outline.addPoint(rect.width - 1, rect.height - 1);
		outline.addPoint(0, rect.height - 1);
		
//		graphics.fillPolygon(outline);
		
		// draw the inner outline
		PointList innerLine = new PointList();
		
		innerLine.addPoint(rect.width - cornerSize - 1, 0);
		innerLine.addPoint(rect.width - cornerSize - 1, cornerSize);
		innerLine.addPoint(rect.width - 1, cornerSize);
		innerLine.addPoint(rect.width - cornerSize - 1, 0);
		innerLine.addPoint(0, 0);
		innerLine.addPoint(0, rect.height - 1);
		innerLine.addPoint(rect.width - 1, rect.height - 1);
		innerLine.addPoint(rect.width - 1, cornerSize);
		
		
		graphics.pushState();
		graphics.setForegroundColor(((ColorManager)(ResourceGroupManager.SINGLETON.getManager(ResourceGroupManager.COLOR_RESOURCE))).getSWTColor(255, 255, 255));
		graphics.fillGradient(rect, true);
		graphics.popState();
		
		graphics.translate(getLocation());
		graphics.drawPolygon(innerLine);
		graphics.translate(getLocation().getNegated());
		
//		graphics.drawLine(new Point(rect.width - 1-cornerSize,0),new Point(0,0));
//		graphics.setForegroundColor(new Color(null,0,0,0));
		
	}
	
	public TextFlow getBody() {
		return textFlow;
	}
	public int getWidth(){
		
		return width;
	}
	public void setBody(String body) {
		textFlow.setText(body);
	}
}
